﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;

namespace FTP客户端
{
    public partial class AnotherForm : Form
    {
        private LoginWindow parent;
        public string ftpUserID;
        public string ftpPassword;
        public string ftpServerIP;
        public AnotherForm(LoginWindow parent)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.parent = parent;
        }

        private void AnotherForm_Load(object sender, EventArgs e)
        {

        }

        private void serverIPLabel_Click(object sender, EventArgs e)
        {

        }

        private void usernameLabel_Click(object sender, EventArgs e)
        {

        }

        private void passwordLabel_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            parent.Show();
            this.Hide();
        }

        private void passwordTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void usernameTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void LoginButton_Click_1(object sender, EventArgs e)
        {
            string ipPattern = @"^(\d{1,3}\.){3}\d{1,3}$";
            if (!Regex.IsMatch(serverIPTextBox.Text, ipPattern))
            {
                MessageBox.Show("请输入有效的IP地址");
                return;
            }
            if (string.IsNullOrEmpty(usernameTextBox.Text))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(passwordTextBox.Text))
            {
                MessageBox.Show("密码不能为空");
                return;
            }

            ftpServerIP = serverIPTextBox.Text;
            ftpUserID = usernameTextBox.Text;
            ftpPassword = passwordTextBox.Text;

            try
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create($"ftp://{ftpServerIP}");
                request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                response.Close();
                MessageBox.Show("连接成功");
                MainWindow mainWindow = new MainWindow(parent);
                mainWindow.Show();
                this.Hide();
            }
            catch (WebException ex)
            {
                MessageBox.Show($"连接失败: {ex.Message}");
            }
        }

        private void serverIPTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}


